package com.neu.cloudfactory.service.imp;

import com.neu.cloudfactory.bean.Capacity.Capactity;
import com.neu.cloudfactory.repository.CapactityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

@Service
public class Capactityserviceimpl implements com.neu.cloudfactory.service.Capactityservice {
    @Autowired
    CapactityRepository capactityRepository;

    @Override
    public List<Capactity> findAllByEquipmentidAndFactoryid(Integer equipmentid, Integer factoryid){
        return capactityRepository.findAllByEquipmentidAndFactoryid(equipmentid, factoryid);
    }

    @Override
    public String add(Capactity capactity){
        boolean complete = false;
        try {
            Optional<Capactity> tmp = capactityRepository.findByEquipmentidAndFactoryidAndProductid(capactity.getEquipmentid(),capactity.getFactoryid(),capactity.getProductid());
            if(tmp.isPresent()){
                capactity.setId(tmp.get().getId());
            }
            capactity = capactityRepository.save(capactity);
            complete = capactity.getId()!=null;
        }catch (Exception e){

        }
        return complete?"success":"failed";
    }
    @Override
    public String update(Capactity capactity){
        boolean complete = false;
        try {
            capactity = capactityRepository.save(capactity);
            complete = capactity.getId()!=null;
        }catch (Exception e){

        }
        return complete?"success":"failed";
    }

    @Override
    public String deleteById(Integer id){
        boolean complete = false;
        try{
            capactityRepository.deleteById(id);
            complete = true;
        }catch (Exception e){

        }
        return complete?"success":"failed";
    }
}
